import { dictFullInfo } from '@/api/login-base'
import defaultSettings from '@/settings'

function setDefaultArr() {
  let arrObj = {}
  for (let key in defaultSettings.presetDictionary){
    let arr = []
    for (let dKey in defaultSettings.presetDictionary[key]){
      arr.push({value: dKey, label: defaultSettings.presetDictionary[key][dKey], type: key})
    }
    arrObj[key] = arr
  }
  return arrObj
}
const state = {
  dict: defaultSettings.presetDictionary,
  dictArr: setDefaultArr(), //原始数据字典顺序，转对象后会丢失原始排序，存成数组可避免此问题
};

const mutations = {
  SET_DICT: (state, {key, info, arr}) => {
    // Object.assign(state.dict, {key})
    state.dict[key] = info
    state.dictArr[key] = arr
  },
};

const actions = {
  getFullDictionary({commit}){
    return new Promise((resolve, reject) => {
      dictFullInfo()
        .then(res => {
          for(const key in res) {
            const info = {};
            res[key].forEach(item => {
              info[item.value] = item.label
            });
            commit('SET_DICT', {key, info, arr: res[key]});
          }
          resolve()
        })
    })
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
